package com.hs.shirodemo2.controller;
import com.hs.shirodemo2.service.FileUploadService;
import com.hs.shirodemo2.utils.MessageSendUtil;
import com.hs.shirodemo2.utils.Result;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * @ClassName FileDownloadController
 * @Description TODO
 * @Author 郭敖
 * @Date 2023年08月16日 19:29
 * @Version 1.0
 */
@Controller
public class FileDownloadController {

    @Autowired
    private FileUploadService fileUploadService;

    @GetMapping("/image")
    public void downloadImage(@RequestParam("path") String path, HttpServletResponse response) throws IOException {
        File file = new File("d:/"+path);
        FileInputStream fin = new FileInputStream(file);
        // 设置响应头信息
        response.setHeader("Content-Disposition", "filename="+path);//预览
        ServletOutputStream out = response.getOutputStream();
        // 拷贝
        IOUtils.copy(fin,out);
    }

    @PostMapping("/upload")
    @ResponseBody
    public Result upload(@RequestBody MultipartFile file) throws IOException {
        String url = fileUploadService.uploadOSSOneFile(file);
        return Result.success(url);
    }

    @GetMapping("/message/{phone}")
    @ResponseBody
    public Result message(@PathVariable String phone) throws Exception {
        MessageSendUtil.sendMessage(phone,"8888");
        return Result.success("8888");
    }
}
